var path = require('path');
var extend = require('util')._extend;
var loader = require('./loader');
var logger = require('./logger');
var parser = require('./parser');
var fs = require('fs');
var util = require('./util');
var conf = require('./conf');
var DATA_FILE = path.join(conf.DATA, 'data_' + util.formatDate());
var BRANDS_FILE = path.join(conf.DATA, 'brands_' + util.formatDate());
var BASE_URL = conf.BASE_URL;
var brands, products;

if (fs.existsSync(DATA_FILE)) {
	try {
		products = fs.readFileSync(DATA_FILE, 'utf8').trim().split(/\r\n|\n|\r/g);
	} catch(e) {}
}


process.once('loaderIdle', function() {
	logger.info('start loading images...');
	brands && fs.writeFileSync(BRANDS_FILE, JSON.stringify(brands));
	if (!products) {
		products = fs.readFileSync(DATA_FILE, 'utf8').trim().split(/\r\n|\n|\r/g);
	}
	process.on('loaderIdle', loadImgs);
	loadImgs();
	
	function loadImgs() {
		if (!products.length) {
			logger.info('--------------------end-----------------------');
			return;
		}
		
		var _products = products.slice(0, 500);
		_products = JSON.parse('[' + _products.join() + ']');
		_products.forEach(function(item) {
			loader.get(item.bigImg, true);
		});
		products = products.slice(500);
	}
});

process.on('uncaughtException', function(err){
	logger.error(err);
	process.exit(1);
});


if (products) {
	process.emit('loaderIdle');
} else {
	try {
		fs.unlinkSync(DATA_FILE);
	} catch(e) {}
	
	loader.get(BASE_URL, function(err, body) {
		if (err) {
			return;
		}
		
		brands = parser.parseBrands(body);
		brands.forEach(function(brand) {
			loader.get(BASE_URL + brand.url, function(err, body) {
				if (err) {
					return;
				}
				
				var categories = parser.parseCategories(body);
				brand.categories = categories;
				if (categories.length) {
					categories.forEach(function(category) {
						loader.get(BASE_URL + category.url, function(err, body) {
							if (err) {
								return;
							}
	
							var subCategories = category.categories = parser.parseSubCategories(body);
							
							if (subCategories.length) {
								subCategories.forEach(function(subCategory) {
									loader.get(BASE_URL + subCategory.url, function(err, body) {
										if (err) {
											return;
										}
										
										loadAllPages(body, subCategory.url, {
											brandUrl: brand.url,
											brandName: brand.name,
											categoryUrl: category.url,
											categoryName: category.name,
											subCategoryUrl: subCategory.url,
											subCategoryName: subCategory.name
										});
									});
								});
							} else {
								loadAllPages(body, category.url, {
									brandUrl: brand.url,
									brandName: brand.name,
									categoryUrl: category.url,
									categoryName: category.name
								});
							}
						});
					});
				} else {
					loadAllPages(body, brand.url, {
						brandUrl: brand.url,
						brandName: brand.name
					});
				}
			});
		});
	});
}

function loadAllPages(body, url, category) {
	var count = parser.parsePageCount(body);
	parseProductList(body, category);
	
	if (count < 2) {
		return;
	}
	
	url = url.replace(/\.html$/, '/p/');
	for (var i = 2; i <= count; i++) {
		loader.get(BASE_URL + url + i + '.html', function(err, body) {
			if (err) {
				return;
			}
			parseProductList(body, category);
		});
	}
}

function parseProductList(body, category) {
	var list = parser.parseProductList(body);
	if (!list.length) {
		return;
	}
	list = list.map(function(item) {
		return JSON.stringify(extend(item, category));
	});
	fs.writeFile(DATA_FILE, '\r\n' + list.join('\r\n'), {
		flag: 'a'
	});
}



